The present invention relates to a relocation format for linking, and in particular to a method of linking, a linker and a computer program product containing relocation instructions.
Linkers for producing executable programs are known. Generally speaking, a linker acts to link a number of object code modules to form a single executable program. Object code modules are usually generated from program source code modules, these modules being written in a high level language. An assembler/compiler reads each source code module and assembles and/or compiles the high level language of the source code module to produce an object code module. The assembler also generates a number of relocations which are used to combine the object code modules at link time in a linker.
The ELF (executable linking format) standard defines a convention for naming relocations belonging to a given section, e.g. rela.abc is relocation section of section space .abc. Standard relocations under the ELF format allow an offset in section data to be defined where patching is to occur and a symbol whose value is to be patched. A type field also exists which is used to describe the appropriate method of encoding the value of the symbol into the instruction or data of the section data being patched. According to the existing arrangements, the relocation type definitions are usually created on an ad hoc basis for each instruction set targeted. The 32 bit ELF standard allows only 256 distinct relocation types, so the same types are reascribed to different semantics for each instruction set.
The existing linkers each have to be set up to understand the particular semantics used for the type definitions in the relocations for each instruction set. Moreover, the relocation operations which can be defined by the relocations are limited in the existing ELF standard.
It is an object of the present invention to increase the flexibility of operation of a linker at link time, in particular in relation to code optimizations for the executable program.
According to an aspect of the present invention there is provided a method of preparing an executable program from a plurality of object code modules, at least one of said object code modules including section data specifying a plurality of code sequences each associated with relocation instructions identifying condition parameters, the method comprising: reading said relocation instructions and determining for each condition parameter whether or not the condition specified for that parameter is satisfied; on the basis of the determination of the condition parameters, selecting only one of said code sequences for inclusion in the executable program; and preparing the executable program including said selected code sequence in the section data.
One of the relocation instructions causes a value for use for determining the condition to be pushed onto a stack.
Another relocation instruction conveys an index so that the top of the stack value can be held in a condition array at the index for subsequent use. To access the value, a fetch relocation instruction conveys the index which allows the value to be recalled from the condition array.
The optional code sequences can be held in a special section (.macro) of the object code module itself, the location of the special section being identified in the relocation instructions. Alternatively, the code sequences can be held within the section data itself where the selected code sequence is to be included.
As a further alternative, the code sequences can be held in library object files separate from the object code module which are read by the linker at link time, the code sequences being specified by the relocation instructions.
The invention provides in a further aspect a linker for preparing an executable program from a plurality of object code modules, each module containing section data specifying a plurality of optional code sequences, each associated with relocation instructions, the linker comprising: a stack for holding values; a relocation module for reading said relocation instructions and carrying out relocation operations on the section data; wherein the relocation module is responsive to a value recalled from the stack to select one of said code sequences in dependence on said value; a section data module for holding section data which is subject to said relocation operations; and a program forming module for preparing the executable program, wherein the program forming module introduces only the selected code sequence into the section data.
The linker can comprise a parameter array holding a set of parameters each associated with a respective index, said parameters modifying said optional code sequences.
The relocation module can comprise means for implementing calculations conveyed by the relocation instructions using the values on the stack.
The linker can include a symbol table which holds values in association with symbols, said symbols being conveyed by the relocation instructions.
In another aspect the invention provides a computer program product in the form of an object code module comprising section data specifying a plurality of code sequences each having relocation instructions associated therewith, wherein the computer program product is cooperable with a linker to resolve conditions conveyed by the relocation instructions based on values held at the linker in order to determine which of said code sequences is to be included in a final executable program formed by the linker.
A still further aspect of the invention provides a method of assembling an object code module for linking to form an executable program, said object code module including section data for inclusion in the executable program and relocation instructions for controlling relocation operation at link time, the method comprising: executing a set of assembler directives and generating said relocation instructions responsive to the assembler directives, said directives including a conditional directive; responsive to the conditional directive, marking a plurality of code sequences in the section data as optional code sequences for conditional inclusion in the executable program, said marking step including generating relocation instructions identifying conditions resolvable at link time to determine which of said code sequences is to be included in the executable program.
The following described embodiment of the present invention allows code sequences to be conditionally included in section data. Conditional assembler directors are provided which instruct the linker to delete section data. In the simplest embodiment, all the alternative sequences are present in the section. This can cause object files to become bloated in the case of commonly used idioms, and therefore it can be useful to locate the alternative code sequences in a special section of the object code module or to rely on library object files.
At link time, the linker makes multiple passes over the sections relocations recording which conditional passages are included. This considerably increases the possibilities of optimization at link time, or linker relaxation.